home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / readers / skim-0.8 / skim-0 / skim-0.8.4 / CheckForDebris.h < prev    next >
C/C++ Source or Header  |  1996-02-18  |  2KB  |  79 lines

  1. /*
  2.  * NAME
  3.  *   CheckForDebris.h
  4.  * DESCRIPTION
  5.  *   A bit of trickery to check for memory leaks. The idea is that the
  6.  *   implementation of every class #defines CLASS_NAME, includes this file
  7.  *   and calls IncrementObjectCount() and DecrementObjectCount() whenever
  8.  *   it creates or destroys an object. When the program exits normally, the
  9.  *   object count of every class is checked and an error is reported if the
  10.  *   count is > 0.
  11.  *
  12.  *   This functionality is only in effect when skim is compiled in its
  13.  *   development environment (when NDEBUG is not defined).
  14.  * COPYRIGHT
  15.  *   Skim - Off-line news reading package optimized for slow lines.
  16.  *   Copyright (C) 1996  Rene W.J. Pijlman
  17.  *
  18.  *   This program is free software; you can redistribute it and/or modify
  19.  *   it under the terms of the GNU General Public License as published by
  20.  *   the Free Software Foundation; either version 2 of the License, or
  21.  *   (at your option) any later version.
  22.  * 
  23.  *   This program is distributed in the hope that it will be useful,
  24.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.  *   GNU General Public License for more details.
  27.  * 
  28.  *   You should have received a copy of the GNU General Public License
  29.  *   along with this program; if not, write to the Free Software
  30.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  * VERSION
  32.  *   Skim version 0.8.4.
  33.  */
  34.  
  35. #include <stdio.h>
  36.  
  37. #ifdef NDEBUG
  38. #   define IncrementObjectCount()
  39. #   define DecrementObjectCount()
  40. #else
  41.  
  42.     static Boolean ObjectCountInitialized = False;
  43.     static Natural NumberOfObjects = 0L;
  44.  
  45. #   define IncrementObjectCount() \
  46.     { \
  47.     if (!ObjectCountInitialized)  InitializeObjectCount(); \
  48.         NumberOfObjects++; \
  49.     }
  50.  
  51. #   define DecrementObjectCount() \
  52.     { \
  53.     if (!ObjectCountInitialized)  InitializeObjectCount(); \
  54.         assert(NumberOfObjects > 0); \
  55.     NumberOfObjects--; \
  56.     }
  57.  
  58.     static void CheckForDebris( int ExitStatus, void * ClientData )
  59.     {
  60.     if ( ExitStatus == EXIT_SUCCESS && NumberOfObjects != 0 )
  61.     {
  62.         fprintf( stderr,
  63.              "Warning: %lu %s object%s not destroyed\n",
  64.              NumberOfObjects, CLASS_NAME,
  65.              NumberOfObjects > 1 ? "s were" : " was" );
  66.     }
  67.     }
  68.  
  69.     static void InitializeObjectCount( void )
  70.     {
  71.     if ( on_exit( CheckForDebris, NULL ) != 0 )
  72.     {
  73.         fprintf( stderr, "Cannot register function with on_exit()\n" );
  74.     }
  75.  
  76.     ObjectCountInitialized = True;
  77.     }
  78. #endif
  79.